<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<title>N6_1F储位管理</title>
<%@include file="/WEB-INF/common/css.jsp"%>
<style>
.xtcardshow {
	width: 80%;
	height: 40px;
	color: #fff;
	text-align: center;
	line-height: 40px;
	border-radius: 50px;
}
</style>
<%@include file="/WEB-INF/common/topjs.jsp"%>
</head>
<body class="layui-layout-body">
	<div class="layui-layout layui-layout-admin">

		<%@include file="/WEB-INF/common/jsp/header.jsp"%>

		<!-- 内容主体区域 -->
		<div class="layui-container" style="padding: 15px;">
			<div class="layui-row">
				<div class="layui-col-md4" style="padding: 5px">
					<div class="layui-card">
						<div class="layui-card-header layui-bg-green">用户信息</div>
						<div class="layui-card-body">
							<p>工号: ${sessionScope.user.no }</p>
							<p>用户名: ${sessionScope.user.username }</p>
						</div>
					</div>
					<div class="layui-card" style="display: none">
						<div class="layui-card-header layui-bg-green">操作</div>
						<div class="layui-card-body">
							<p>
								<button class="layui-btn layui-btn-radius">更改信息</button>
								<button class="layui-btn layui-btn-radius layui-btn-normal">更改密码</button>
							</p>
						</div>
					</div>
					<div class="layui-card">
						<div class="layui-card-header"
							style="background: #5FB878; color: #fff">操作面板</div>
						<div class="layui-card-body">
							<form class="layui-form" action="" lay-filter="">
								<div class="layui-form-item">
								    <label class="layui-form-label">卡住料号</label>
								    <div class="layui-input-block">
								      <input type="checkbox" id="kaizhu" name="kaizhu" lay-skin="switch" lay-text="开启|关闭" lay-filter="kaizhu" checked>
								    </div>
								</div>
							</form>
						</div>
					</div>
					<%@include file="/WEB-INF/common/jsp/message.jsp" %>
				</div>
				<div class="layui-col-md8" style="padding: 5px">
					<div class="layui-card">
						<div class="layui-card-header" style="background-color:#00bcd4;color:#fff">箱子 </div>
						<div class="layui-card-body">
							<h3><button class="layui-btn" style="background:#f357c3" onclick="showBenBox()">查看本箱已刷入</button> &nbsp; &nbsp; 本箱编号: <span id="boxNo" style="color:#9c27b0;font-weight:1000">#${obj.box }</span>  &nbsp; &nbsp; 当前数量: <span id="boxNum" style="color:#4caf50;font-weight:1000">${obj.num}</span></h3>
						</div>
					</div>
					<div class="layui-card">
						<div class="layui-card-header layui-bg-cyan">先进先出 自动登记 </div>
						<div class="layui-card-body">
							<hr>
							<form class="layui-form" action="" lay-filter="form">
								<div class="layui-form-item">
									<label class="layui-form-label">SN</label>
									<div class="layui-input-block">
										<input type="text" name="sn" required lay-verify="required"
											lay-filter="snInput" placeholder="SN" autocomplete="off"
											class="layui-input" id="sn" value="">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">料号</label>
									<div class="layui-input-block">
										<input type="text" name="liaohao" id="liaohao" required
											lay-verify="required" placeholder="料号" autocomplete="off"
											class="layui-input" value="">
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">机种</label>
									<div class="layui-input-block">
										<select name="dataMachineId" id="machine"
											lay-verify="required" lay-filter="machine">

										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">类型</label>
									<div class="layui-input-block">
										<select name="dataItemId" id="item" lay-verify="required"
											lay-filter="item">

										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">状态</label>
									<div class="layui-input-block">
										<select name="dataGJEHStateId" id="gjehState"
											lay-verify="required" lay-filter="gjehState">

										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">暂放状态</label>
									<div class="layui-input-block">
										<select name="dataTempputStateId" id="tempputState"
											lay-verify="required" lay-filter="tempputState">

										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">钢架编号</label>
									<div class="layui-input-block">
										<select name="gjNo" id="gjNo" lay-verify="required"
											lay-filter="gjNo">

										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">大储位</label>
									<div class="layui-input-block">
										<select name="daChuwei" id="daChuwei" lay-verify="required"
											lay-filter="daChuwei">

										</select>
									</div>
								</div>
								<div class="layui-form-item">
									<label class="layui-form-label">小储位</label>
									<div class="layui-input-block">
										<select name="xiaoChuwei" id="xiaoChuwei"
											lay-verify="required" lay-filter="xiaoChuwei">

										</select>
									</div>
								</div>
								<div class="layui-form-item" style="display:none">
									<div class="layui-input-block">
										<button class="layui-btn" lay-submit lay-filter="in">登记</button>
										<button type="reset" class="layui-btn layui-btn-primary">重置</button>
									</div>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>

		<%@include file="/WEB-INF/common/jsp/footer.jsp"%>

	</div>
	<%@include file="/WEB-INF/common/bottomjs.jsp"%>
				
	<table id="showTable" class="layui-table" style="display:none;">
		<tr><th>机种</th><td>料号开头</td> <th>机种</th><td>料号开头</td></tr>
		<tr><th>Chariot-TB</th><td>70E5291ZZ...</td> <th>Chariot-BD</th><td>70E5291KZ...</td></tr>
		<tr><th>Vertex</th><td>70E52D...</td> <th>Castle</th><td>70E51T...</td></tr>
		<tr><th>Bishop</th><td>70E51Z...</td> <th>Rook</th><td>70E525...</td></tr>
		<tr><th>Calgary</th><td>70E530...</td> <th>Katana-TB</th><td>70E52X1PZ...</td></tr>
		<tr><th>Katana-OR</th><td>70E52X...</td> <th></th><td></td></tr>
	</table>
	
	<table id="benbox" class="layui-table" style="display:none;">
		
	</table>
	
	<script>
		var isKazhu = false; //true 为料号机种卡住， 否则不卡
		$(function() {
			initForm();
			initCaozuoPanel();
			checkShangyiciShibushiWan();
		})
		function checkShangyiciShibushiWan(){
			
		}
		function newMessage(){
			layer.open({
				type: 1,
				title:"卡关详情",
				content: $('#showTable'),
				area: ['500px', '500px'],
				cancel: function(e){
					$("#showTable").hide();
				}
			});
		}
		function showBenBox(){
			$.ajax({
				url: "${base}/api/dataDetails/auto/boxlist.api",
				type: "post",
				headers: Apicheck.getHeaders(),
				success: function(res){
					if(res.code == 1001){
						var html = ""
						console.log(res.data);
						html += "<tr>";
						html += "<th>SN</th>"
						html += "<th>料号</th>"
						html += "</tr>"
						for(var i = 0; i < res.data.length; i ++){
							html += "<tr>";
							html += "<td>" + res.data[i].sn + "</td>"
							html += "<td>" + res.data[i].liaohao + "</td>"
							html += "</tr>"
						}
						$('#benbox').html(html);
						layer.open({
							type: 1,
							title:"这个箱子里已经刷了这些机台在缓存了",
							content: $('#benbox'),
							area: ['500px', '500px'],
							cancel: function(e){
								$("#benbox").hide();
							}
						});
					}
				}
			});
		}
		function initForm() {
			initSNInput();
			findMachines();
			findItems();
			findGjehStates();
			findTempputStates();

			findGanjia();
			//findDachuwei();
			//findXiaochuwei();
		}
		
		function initCaozuoPanel(){
			if($("#kaizhu").val() == "on"){
				isKazhu = true;
			}
		
			layui.form.on("switch(kaizhu)", function(data){
				isKazhu = data.elem.checked;
			})
		}
		function initSNInput() {
			$("#sn").focus();
			$('#sn').bind('input propertychange', function() {
				var sn = $("#sn").val();
				
				if (sn.length == 12) {
					var reg = /^[\d]+$/;
					if(!reg.test(sn)){
						var imgSrc = ["0601.jpg", "11.gif", "14.gif", "16.gif", "21.gif", "4.gif"];
						var i = Math.floor(Math.random() * imgSrc.length);
						var conHtml = "<img src='${base}/assets/image/alertemoj/"+imgSrc[i]+"' /><br>";
						conHtml += "<h3 style='text-align: center;font-size: 23px;margin-top: 27px;color:#d00d0d;font-weight: bold'>SN格式有误</h3>";
						layer.open({
							title : "消息提示",
							content : conHtml,
						});
						return;
					}
					var info = checkInput();
					if( info != "OK"){
						$("#sn").val("");
						var imgSrc = ["0601.jpg", "11.gif", "14.gif", "16.gif", "21.gif", "4.gif"];
						var i = Math.floor(Math.random() * imgSrc.length);
						var conHtml = "<img src='${base}/assets/image/alertemoj/"+imgSrc[i]+"' /><br>";
						conHtml += "<h3 style='text-align: center;font-size: 23px;margin-top: 27px;color:#d00d0d;font-weight: bold'>" + info + "</h3>";
						layer.open({
							title : "消息提示",
							content : conHtml,
						});
						return;
					}
					var hqlhHtml = "<img src='${base}/assets/image/img1.gif' /><br>";
					hqlhHtml += "<h3 style='text-align: center;font-size: 23px;margin-top: 27px;color:#149e5e;font-weight: bold'>系统正在努力工作中，请稍后</h3>";
					
					var hqlhAreat = layer.open({
						title : '消息提示',
						content : hqlhHtml,
						btn:[],
						closeBtn: 0
					});
					
					$.ajax({
						url : "${base}/api/http/getLiaohao.api",
						type : "post",
						data : {
							sn : sn
						},
						headers: Apicheck.getHeaders(),
						success : function(result) {
							if (result.code == 1001) {
								$("#liaohao").val(result.data);
								var kazhu = kazhuliaohao(result.data);
								if(kazhu != "NA"){
									layer.close(hqlhAreat);
									var imgSrc = ["0601.jpg", "11.gif", "14.gif", "16.gif", "21.gif", "4.gif"];
									var i = Math.floor(Math.random() * imgSrc.length);
									var conHtml = "<img src='${base}/assets/image/alertemoj/"+imgSrc[i]+"' /><br>";
									conHtml += "<h3 style='text-align: center;font-size: 23px;margin-top: 27px;color:#d00d0d;font-weight: bold'>"+kazhu+"</h3>";
									layer.open({
										title : "获取料号",
										content : conHtml,
										btn:[],
										cancel: function(index, layero){
											$("#sn").val("");
											$("#sn").focus();
										}
									});
								}else{
									$.ajax({
										url : "${base}/api/dataDetails/auto/xjxcin.api",
										type : "post",
										data : {
											sn : $("#sn").val(),
											liaohao: $("#liaohao").val(),
											dataMachine : $("#machine").val(),
											dataItem : $("#item").val(),
											dataGJEHState : $("#gjehState").val(),
											dataTempputState : $("#tempputState").val(),
											gjNo : $("#gjNo").val(),
											daChuwei : $("#daChuwei").val(),
											xiaoChuwei : $("#xiaoChuwei").val(),
										},
										headers: Apicheck.getHeaders(),
										success : function(result) {
											layer.close(hqlhAreat);
											if(result.code == 1001){
												//$("#liaohao").val("");
												$("#sn").val("");
												$("#sn").focus();
												$("#boxNo").html("#" + result.data.box);
												$("#boxNum").html(result.data.num)
												if(!result.data.code){
													layer.msg(result.msg, {icon: 6}); 
												}else{
													man10tainiao();
												}
											}else{
												//layer.msg(result.msg, {icon: 5}); 
												layer.open({
													title : "噢噢，报错了",
													content : result.msg,
												})
											}
										}, 
										error: function(a, b, c){
											layer.close(hqlhAreat);
										}
									})
								}
								
							} else {
								layer.close(hqlhAreat);
								var imgSrc = ["0601.jpg", "11.gif", "14.gif", "16.gif", "21.gif", "4.gif"];
								var i = Math.floor(Math.random() * imgSrc.length);
								var conHtml = "<img src='${base}/assets/image/alertemoj/"+imgSrc[i]+"' /><br>";
								conHtml += "<h3 style='text-align: center;font-size: 23px;margin-top: 27px;color:#d00d0d;font-weight: bold'>" + result.msg + "</h3>";
								layer.open({
									title : "获取料号",
									content : conHtml,
								});
							}
						}
					});
				}
			});
		}
		function  autoMode(){
			layer.msg("当前已经是自动模式了", {icon: 5});
		}
		function  shoudongMode(){
			layer.msg("手动模式还没有实现", {icon: 5});
		}
		
		function man10tainiao(){
			var msg = "<img src='${base}/assets/image/img1.gif' /><br>";
			msg += "<h3 style='text-align: center;font-size: 23px;margin-top: 27px;color:#149e5e;font-weight: bold'>这个箱子已经满了</h3>";
			layer.msg("5555555555555");
			layer.open({
				title : '消息提示',
				content : msg,
				btn:["写入数据库", "清空"],
				closeBtn: 0,
				yes:function(index, layero){
					$.post("${base}/api/dataDetails/auto/writeDB", {}, function(res){
						layer.msg(res.msg);
						if(res.code == 1001){
							$("#boxNo").html("#" + res.data.box);
							$("#boxNum").html(res.data.num)
						}
					});
				},
				btn2: function(index, layero){
					$.post("${base}/api/dataDetails/auto/clearCache", {}, function(res){
						layer.msg(res.msg);
						if(res.code == 1001){
							$("#boxNo").html("#" + res.data.box);
							$("#boxNum").html(res.data.num)
						}
					});
				}
			});
		}
		
		function kazhuliaohao(liaohao){
			if(liaohao[0] == "9"){
				return "料号是9开头了, 不让登记了哈";
			}
			if(isKazhu){
				if(liaohao.startsWith("70E52X")){
					if(liaohao.startsWith("70E52X1PZ")){
						if($("#machine").val() != "Katana-TB"){
							return "70E52X1PZ料号开头的是Katana-TB,你选择机种是" + $("#machine").val();
						}
					}
					if($("#machine").val() != "Katana-BD"){
						return "70E52X料号开头的是Katana-BD,你选择机种是" + $("#machine").val();
					}
				}else if(liaohao.startsWith("70E5291ZZ")){
					if($("#machine").val() != "Chariot-TB"){
						return "70E5291ZZ料号开头的是Chariot-TB,你选择机种是" + $("#machine").val();
					}
				}else if(liaohao.startsWith("70E5291KZ")){
					if($("#machine").val() != "Chariot-BD"){
						return "70E5291KZ料号开头的是Chariot-BD,你选择机种是" + $("#machine").val();
					}
				}else if(liaohao.startsWith("70E52D")){
					if($("#machine").val() != "Vertex"){
						return "70E52D料号开头的是Vertex,你选择机种是" + $("#machine").val();
					}
				}else if(liaohao.startsWith("70E51T")){
					if($("#machine").val() != "Castle"){
						return "70E51T料号开头的是Castle,你选择机种是" + $("#machine").val();
					}
				}else if(liaohao.startsWith("70E51Z")){
					if($("#machine").val() != "Bishop"){
						return "70E51Z料号开头的是Bishop,你选择机种是" + $("#machine").val();
					}
				}else if(liaohao.startsWith("70E525")){
					if($("#machine").val() != "Rook"){
						return "70E525料号开头的是Rook,你选择机种是" + $("#machine").val();
					}
				}else if(liaohao.startsWith("70E530")){
					if($("#machine").val() != "Calgary"){
						return "70E530料号开头的是Calgary,你选择机种是" + $("#machine").val();
					}
				}
			}
			return "NA";
		}
		function findMachines() {
			$.ajax({
				url : "${base}/api/dataMachine/list.api",
				type : "post",
				data : {
					page : 1,
					size : 1000
				},
				headers: Apicheck.getHeaders(),
				success : function(result) {
					$("#machine").html("");
					$("#machine").append("<option value=''></option>");
					var list = result.data.list;
					for (var i = 0; i < list.length; i++) {
						$("#machine").append(
								"<option value='"+list[i].name+"'>"
										+ list[i].name + "</option>");
					}
					layui.form.render("select", "form");
				}
			});
		}
		function findItems() {
			$.ajax({
				url : "${base}/api/dataItem/list.api",
				type : "post",
				data : {
					page : 1,
					size : 1000
				},
				headers: Apicheck.getHeaders(),
				success : function(result) {
					$("#item").html("");
					$("#item").append("<option value=''></option>");
					var list = result.data.list;
					for (var i = 0; i < list.length; i++) {
						$("#item").append(
								"<option value='"+list[i].name+"'>"
										+ list[i].name + "</option>");
					}
					layui.form.render("select", "form");
				}
			});
		}
		function findGjehStates() {
			$.ajax({
				url : "${base}/api/dataGJEHState/list.api",
				type : "post",
				data : {
					page : 1,
					size : 1000
				},
				headers: Apicheck.getHeaders(),
				success : function(result) {
					$("#gjehState").html("");
					$("#gjehState").append("<option value=''></option>");
					var list = result.data.list;
					for (var i = 0; i < list.length; i++) {
						$("#gjehState").append(
								"<option value='"+list[i].name+"'>"
										+ list[i].name + "</option>");
					}
					layui.form.render("select", "form");
				}
			});
		}
		function findTempputStates() {
			$.ajax({
				url : "${base}/api/dataTempputState/list.api",
				type : "post",
				data : {
					page : 1,
					size : 1000
				},
				headers: Apicheck.getHeaders(),
				success : function(result) {
					$("#tempputState").html("");
					$("#tempputState").append("<option value=''></option>");
					var list = result.data.list;
					for (var i = 0; i < list.length; i++) {
						$("#tempputState").append(
								"<option value='"+list[i].name+"'>"
										+ list[i].name + "</option>");
					}
					layui.form.render("select", "form");
				}
			});
		}

		function findGanjia() {
			$.ajax({
				url : "${base}/api/gangjia/list.api",
				type : "post",
				data : {
					page : 1,
					size : 1000
				},
				headers: Apicheck.getHeaders(),
				success : function(result) {
					$("#gjNo").html("");
					$("#gjNo").append("<option value=''></option>");
					var list = result.data.list;
					for (var i = 0; i < list.length; i++) {
						$("#gjNo").append(
								"<option value='"+list[i].name+"'>"
										+ list[i].name + "</option>");
					}
					layui.form.render("select", "form");
					layui.form.on("select(gjNo)", function(data) {
						$("#daChuwei").html("");
						$("#xiaoChuwei").html("");
						$("#daChuwei").val("");
						$("#xiaoChuwei").val("");
						findDachuwei();
					});
				}
			});
		}

		function findDachuwei() {
			$.ajax({
				url : "${base}/api/dachuwei/findByGanjias.api",
				type : "post",
				data : {
					ganjiaName : $("#gjNo").val()
				},
				headers: Apicheck.getHeaders(),
				success : function(result) {
					$("#daChuwei").html("");
					$("#daChuwei").append("<option value=''></option>");
					var list = result.data.dachuweis;
					for (var i = 0; i < list.length; i++) {
						$("#daChuwei").append(
								"<option value='"+list[i].name+"'>"
										+ list[i].name + "</option>");
					}
					layui.form.render("select", "form");
					layui.form.on("select(daChuwei)", function(data) {
						$("#xiaoChuwei").html("");
						$("#xiaoChuwei").val("");
						findXiaochuwei();
					});
				}
			});
		}
		function findXiaochuwei() {
			$.ajax({
				url : "${base}/api/xiaochuwei/findByDachuwei.api",
				type : "post",
				data : {
					dachuweiName : $("#daChuwei").val()
				},
				headers: Apicheck.getHeaders(),
				success : function(result) {
					$("#xiaoChuwei").html("");
					$("#xiaoChuwei").append("<option value=''></option>");
					var list = result.data.xiaochuwei;
					for (var i = 0; i < list.length; i++) {
						$("#xiaoChuwei").append(
								"<option value='"+list[i].name+"'>"
										+ list[i].name + "</option>");
					}
					layui.form.render("select", "form");
				}
			});
		}
		function checkInput(){
			if($("#machine").val() == ""){
				return "呵呵呵呵，你没有选择机种";
			}
			if($("#item").val() == ""){
				return "类型是不是又忘记选了";
			}
			if($("#gjehState").val() == ""){
				return "状态呢？咋又不选了？";
			}
			if($("#tempputState").val() == ""){
				return "暂放状态，暂放状态，暂放状态？";
			}
			if($("#gjNo").val() == ""){
				return "这个...那个...钢架编号是不是没选则呀？";
			}
			if($("#daChuwei").val() == ""){
				return "额。。。看看是不是大储位有没有选择";
			}
			if($("#gjNo").val() == "D01" && $("#xiaoChuwei").val() == ""){
				return "呵呵呵，钢架编号D01有小储位, 你估计又是没有选择小储位";
			}
			var pandianMessage = "";
			$.ajax({
		        "url": "http://172.22.249.119:2818/pileoperation/index.api", 
		        "type":"post",
		        "async": false,
		        "data": {"sn": $("#sn").val(), station:"70house"},
		        'headers': Apicheck.getHeaders(),
		        "success": function(res){
		            if(res.Status == 2){
		            	layer.msg("第三方系统提示: " + res.Msg)
		            	pandianMessage = "OK";
		            }else{
		            	pandianMessage = "第三方系统提示: " + res.Msg;
		            }
		    	}
		    });
			return pandianMessage;
		}
	</script>
</body>
</html>